﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class FinanceCenter_VirtualMoneyList : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            BindData();              //梆定所有数据
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestCompetences = Competences.BuildCompetencesList(Competences.Finance);
        RequestLoginPage = "FinanceCenter/VirtualMoneyList.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.tbSearch.Text.Trim().Equals(""))
        {
            ShoveEL._Web.JavaScript.Alert(this, "请输入要搜索的会员ID");

            return;
        }

        User user = new User();
        user.Name = this.tbSearch.Text.Trim();
        string ret = "";
        int result = user.GetUserInfoByName(ref ret);

        if (result == -1)
        {
            ShoveEL._Web.JavaScript.Alert(this, "此会员不存在");
            return;
        }
        
        dt = DAL.Views.V_Users.Select("ID,[Name],RealityName,Sex,IDCardNumber,VirtualMoney,BV", "[ID] = " + user.ID.ToString(), "ID");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void btnSearchAll_Click(object sender, EventArgs e)
    {
        this.g.CurrentPageIndex = 0;
        this.gPager.PageIndex = 0;

        BindData();
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns[0].ColumnName = "序号";
            dt.Columns[1].ColumnName = "用户名";
            dt.Columns[2].ColumnName = "真实姓名";
            dt.Columns[3].ColumnName = "性别";
            dt.Columns[4].ColumnName = "身份证";
            dt.Columns[5].ColumnName = "电子货币余额";
            dt.Columns[6].ColumnName = "返还货币余额";
      
            DataTableToExcel.DataTableToExcelAndDownload(dt, "VirtualMoneyList.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "ShopSale_Search");
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        double VirtualMoney = 0;        //总电子货币额
        double BV = 0;                  //总返还货币

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                VirtualMoney += ShoveEL._Convert.StrToDouble(dt.Rows[i]["VirtualMoney"].ToString(), 0);
                BV += ShoveEL._Convert.StrToDouble(dt.Rows[i]["BV"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + VirtualMoney + "</td>");
            output.Write("<td align='center'>" + BV + "</td>");
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //DataRowView drv = (DataRowView)e.Item.DataItem;
            //DataRow dr = drv.Row;

            //double VirtualMoney = dr["VirtualMoney"].ToString();   
        }
    }

    /// <summary>
    /// 梆定条件查询出来的数据
    /// </summary>
    private void BindData()
    {
        dt = DAL.Views.V_Users.Select("ID,[Name],RealityName,Sex,IDCardNumber,VirtualMoney,BV", "", "ID");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }
}
